package com.lijinjiang;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @ClassName MyBatisDemo
 * @Description Mybatis 快速入门代码
 * @Author Li
 * @Date 2022/8/10 17:11
 * @ModifyDate 2022/8/10 17:11
 * @Version 1.0
 */
public class MyBatisDemo {
    public static void main(String[] args) throws IOException {
        // 1.加载 mybatis 的核心配置代码，获取 SqlSessionFactory
        String resource = "mybatis.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // 2.获取 SqlSession 对象，用来执行 sql
        SqlSession sqlSession = sqlSessionFactory.openSession();

        // 3.执行 sql
        List<Object> users = sqlSession.selectList("test.selectAll");

        System.out.println(users);

        // 4.释放资源
        sqlSession.close();
    }
}
